﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.GuaZhiDuanLianService;
using CommunitySystem.PageView;

namespace CommunitySystem
{
    public partial class GuaZhiQiManFuJianWindow : ChildWindow
    {
        private int ID;
        private GuaZhiEmpModel model = new GuaZhiEmpModel();
        private GuaZhiDuanLianServiceClient Client1 =
            new GuaZhiDuanLianServiceClient();
        public GuaZhiQiManFuJianWindow(int id)
        {
            InitializeComponent();
            ID = id;
            model.ID = id;
            ObservableCollection<int> ids = new ObservableCollection<int>();
            ids.Add(ID);
            Client1.GetGuaZhiDuanLianEmpZongModelCompleted += new EventHandler<GetGuaZhiDuanLianEmpZongModelCompletedEventArgs>(Client1_GetGuaZhiDuanLianEmpZongModelCompleted);
            Client1.GetGuaZhiDuanLianEmpZongModelAsync(0, "", "", ids);
            Client1.UpdateGuaZhi_ZhengShenCompleted += new EventHandler<UpdateGuaZhi_ZhengShenCompletedEventArgs>(Client1_UpdateGuaZhi_ZhengShenCompleted);

        }

        void Client1_GetGuaZhiDuanLianEmpZongModelCompleted(object sender, GetGuaZhiDuanLianEmpZongModelCompletedEventArgs e)
        {
            Page(null);
            Page1(null);
        }


        private int _maxFileSize = int.MaxValue;

        private FileCollection _files;
        private int _maxUpload = 4;
        private string _customParams;
        private string _fileFilter;
        private void hlb_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = true;

            try
            {
                if (!string.IsNullOrEmpty(_fileFilter))
                    ofd.Filter = _fileFilter;
            }
            catch (ArgumentException ex)
            {
                throw new Exception("Wrong file filter configuration.", ex);
            }

            if (ofd.ShowDialog() == true)
            {

                foreach (FileInfo file in ofd.Files)
                {
                    string GuidName = Guid.NewGuid().ToString("N");
                    string fileName = file.Name;

                    UserFile userFile = new UserFile();
                    userFile.FileName = GuidName; //GuidName file.Name;
                    userFile.FileNameG = fileName;
                    userFile.IsAdd = true;
                    userFile.FileStream = file.OpenRead();


                    #region  页面显示
                    foreach (var item in _files)
                    {
                        if (item.FileNameG.Contains(fileName))
                        {
                            SilverlightMessageBoxLibrary.CustomMessage cm = new SilverlightMessageBoxLibrary.CustomMessage("此文件已上传,需要覆盖原有文件吗?", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
                            cm.Show();
                            cm.OKButton.Click += (sender1, e1) =>
                            {
                                _files.Remove(item);
                            };
                        }
                    }

                    #endregion

                    if (userFile.FileStream.Length <= _maxFileSize)
                    {
                        //向文件列表中添加文件信息
                        if (_files == null) _files = new FileCollection(_customParams, _maxUpload);
                        _files.Add(userFile);
                    }
                    else
                        SilverlightMessageBoxLibrary.Message.InfoMessage("上传最大文件大小：" + (_maxFileSize / 1024).ToString() + "KB.");
                }
            }
        }

        private FileCollection _files1;
        private string _customParams1;
        private string _fileFilter1;
        private void hlb_Click1(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = true;

            try
            {
                if (!string.IsNullOrEmpty(_fileFilter1))
                    ofd.Filter = _fileFilter1;
            }
            catch (ArgumentException ex)
            {
                throw new Exception("Wrong file filter configuration.", ex);
            }

            if (ofd.ShowDialog() == true)
            {

                foreach (FileInfo file in ofd.Files)
                {
                    string GuidName = Guid.NewGuid().ToString("N");
                    string fileName = file.Name;

                    UserFile userFile = new UserFile();
                    userFile.FileName = GuidName; //GuidName file.Name;
                    userFile.FileNameG = fileName;
                    userFile.IsAdd = true;
                    userFile.FileStream = file.OpenRead();


                    #region  页面显示
                    foreach (var item in _files1)
                    {
                        if (item.FileNameG.Contains(fileName))
                        {
                            SilverlightMessageBoxLibrary.CustomMessage cm = new SilverlightMessageBoxLibrary.CustomMessage("此文件已上传,需要覆盖原有文件吗?", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
                            cm.Show();
                            cm.OKButton.Click += (sender1, e1) =>
                            {
                                _files1.Remove(item);
                            };
                        }
                    }

                    #endregion

                    if (userFile.FileStream.Length <= _maxFileSize)
                    {
                        //向文件列表中添加文件信息
                        if (_files1 == null) _files1 = new FileCollection(_customParams1, _maxUpload);
                        _files1.Add(userFile);
                    }
                    else
                        SilverlightMessageBoxLibrary.Message.InfoMessage("上传最大文件大小：" + (_maxFileSize / 1024).ToString() + "KB.");
                }
            }
        }

        public void Page(IDictionary<string, string> initParams)
        {
            _files = new FileCollection(_customParams, _maxUpload);
            if (model.Attachment != null)
            {
                if (model.Attachment.Count > 0)
                {
                    foreach (var item in model.Attachment.Where(p => p.Type == "小结材料"))
                    {
                        var uf = new UserFile() { FileName = item.filename, FileNameG = item.realname, State = Constants.FileStates.Finished, Percentage = Convert.ToInt32(item.Percentage), BytesUploaded = Convert.ToDouble(item.FileSize), FileSize = Convert.ToDouble(item.FileSize) };

                        _files.Add(uf);
                    }
                }
            }

            FileList.ItemsSource = _files;

        }

        public void Page1(IDictionary<string, string> initParams)
        {
            _files1 = new FileCollection(_customParams1, _maxUpload);
            if (model.Attachment != null)
            {
                if (model.Attachment.Count > 0)
                {
                    foreach (var item in model.Attachment.Where(p => p.Type == "考核表"))
                    {
                        var uf = new UserFile() { FileName = item.filename, FileNameG = item.realname, State = Constants.FileStates.Finished, Percentage = Convert.ToInt32(item.Percentage), BytesUploaded = Convert.ToDouble(item.FileSize), FileSize = Convert.ToDouble(item.FileSize) };

                        _files1.Add(uf);
                    }
                }
            }

            FileList1.ItemsSource = _files1;

        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        private readonly string fileDictionary = "file";
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (_files.Count == 0 || _files1.Count == 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请上传材料！");
                return;
            }
            if (_files.Where(p => p.State == Constants.FileStates.Uploading).Count() > 0 || _files1.Where(p => p.State == Constants.FileStates.Uploading).Count() > 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("附件正在上传，请稍后！");
                return;
            }

            if (model.Attachment == null)
                model.Attachment = new System.Collections.ObjectModel.ObservableCollection<GuaZhi_AttatchmentModel>();
            else
                model.Attachment.Clear();
            foreach (var item in _files.Where(p => p.State == Constants.FileStates.Finished).ToList())
            {

                var att = new GuaZhi_AttatchmentModel() { realname = item.FileNameG, filename = item.FileName, Key = fileDictionary, Status = (item.State).ToString(), FileSize = item.FileSize.ToString(), Percentage = item.Percentage.ToString(),Type = "小结材料" };
                model.Attachment.Add(att);
            }
            foreach (var item in _files1.Where(p => p.State == Constants.FileStates.Finished).ToList())
            {

                var att = new GuaZhi_AttatchmentModel() { realname = item.FileNameG, filename = item.FileName, Key = fileDictionary, Status = (item.State).ToString(), FileSize = item.FileSize.ToString(), Percentage = item.Percentage.ToString(), Type = "考核表" };
                model.Attachment.Add(att);
            }
            Client1.UpdateGuaZhi_ZhengShenAsync(model);

        }
        private int count = 1;
        void Client1_UpdateGuaZhi_ZhengShenCompleted(object sender, UpdateGuaZhi_ZhengShenCompletedEventArgs e)
        {
            count = count - 1;
            if (count == 0)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("附件添加成功！");
                this.DialogResult = true;
            }
        }
        private void ibtnFJ_Click(object sender, RoutedEventArgs e)
        {
            if (_files.Count == 0 || _files1.Count == 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请上传材料！");
                return;
            }
            else
            {
                this.OKButton.IsEnabled = true;
                this.NoOKButton.IsEnabled = true;
                _files.UploadFiles();
                _files1.UploadFiles();
            }
        }
    }
}
